<template>
  <div class="login-container">
    <!--设置背景图 -->
    <div class="background">
      <div class="content-box">
        <!-- 左侧图片区域 -->
        <div class="left-box">
          <img src="../../assets/photoes/login/left-img.jpg" alt="无人机" class="drone-img" />
          <div class="company-info">
            <img src="../../assets/photoes/logo.jpg" alt="国家电网 logo" class="grid-logo" />
          </div>
        </div>
        <!-- 右侧登录表单区域 -->
        <div class="right-box">
          <h2 class="title">铁岭电网无人机管理平台</h2>
          <!-- <el-form :model="loginForm" class="login-form">
            <el-form-item>
              <el-input v-model="loginForm.username" placeholder="请输入用户名/手机号" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"
                prefix-icon="el-icon-lock"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn" @click="handleLogin">登录</el-button>
            </el-form-item>
          </el-form> -->
          <el-form :model="loginForm" class="login-form">
            <!-- 账号输入框：绑定 accountNumber -->
            <el-form-item>
              <el-input v-model="loginForm.accountNumber" placeholder="请输入账号" prefix-icon="User"></el-input>
            </el-form-item>

            <!-- 密码输入框：绑定 passWord -->
            <el-form-item>
              <el-input v-model="loginForm.passWord" type="password" placeholder="请输入密码" prefix-icon="Lock"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button class="login-btn" @click="handleLogin">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<!-- <script setup>
import router from "@/router";
import { reactive } from "vue";
import request from '@/utils/request' // 引入全局请求配置
const loginForm = reactive({
  username: "",
  password: "",
});

const handleLogin = (data) => {
  // 调用接口：
  // loginApi(loginForm).then(res => {
  //   // 登录成功处理，如存储 token、跳转页面等
  // }).catch(err => {
  //   // 登录失败处理，如提示错误
  // })
  return request({
    url: '/tielingpowergriduavservice/users/login', // 后端登录接口地址
    method: 'post',
    data // 请求体数据（对应后端 @RequestBody）
  })
  // console.log("点击了登录，实际需补充接口调用等逻辑", loginForm);
  // router.push("/home");
};
</script> -->

<script setup>
import router from "@/router";
import { reactive } from "vue";
import { loginApi } from "@/api/users"; // 引入登录接口
import { ElMessage } from "element-plus"; // 引入 Element Plus 消息组件

// 登录表单数据（注意：后端接收的是 accountNumber 和 passWord，需与后端字段匹配）
const loginForm = reactive({
  accountNumber: "", // 对应后端 UsersVo 的 accountNumber
  passWord: ""       // 对应后端 UsersVo 的 passWord
});

const handleLogin = async () => {
  // 表单验证（简单验证）
  if (!loginForm.accountNumber) {
    ElMessage.warning("请输入账号");
    return;
  }
  if (!loginForm.passWord) {
    ElMessage.warning("请输入密码");
    return;
  }

  try {
    // 调用登录接口
    const res = await loginApi(loginForm);
    // 登录成功处理
    ElMessage.success("登录成功");

    // 存储用户信息到本地（如 localStorage）
    localStorage.setItem("userInfo", JSON.stringify(res.data));
    // 如果后端返回 token，也需要存储
    // localStorage.setItem("token", res.data.token);

    // 跳转到首页
    router.push("/home");
  } catch (error) {
    // 错误处理（接口返回错误已在响应拦截器处理，这里可做额外逻辑）
    console.error("登录失败", error);
  }
};
</script>
<style scoped>
.login-container {
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.background {
  width: 100%;
  height: 100%;
  background: url("../../assets/photoes/login/all-background.jpg") no-repeat center center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.content-box {
  width: 1000px;
  height: 500px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  display: flex;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.left-box {
  width: 40%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #fff;
  position: relative;
}

.drone-img {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.company-info {
  display: flex;
  align-items: center;
  position: absolute;
  left: 30px;
  top: 10px;
  /* margin: 10px; */
  background-color: white;
  width: 180px;
  height: 60px;
  border-radius: 10px;
}

.grid-logo {
  width: auto;
  height: 40px;
  margin-left: 10px;
}

.company-text {
  font-size: 16px;
  color: #333;
}

.right-box {
  width: 60%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 0 50px;
}

.title {
  font-size: 24px;
  color: #31bfae;
  margin-bottom: 30px;
}

.login-form {
  width: 80%;
}

.login-btn {
  width: 100%;
  background-color: #31bfae;
  color: white;
  border-radius: 8px;
}
</style>
